package com.kitty.game.task.service.targetHandler;

import com.kitty.game.role.model.Role;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.config.TaskSet;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.fight.factory.FightFactory;
import com.kitty.game.task.model.product.TaskTarget;
import com.kitty.game.task.model.product.TaskTargetType;
import com.kitty.game.task.model.product.targetparam.FightWinTargetParam;
import com.kitty.game.task.model.user.RoleTask;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.game.task.service.taskHandler.PetFlyTaskHandler;
import com.kitty.game.utils.Const;
import com.kitty.listener.BaseGameEvent;
import com.kitty.listener.event.FightEndEvent;


/**战斗胜利处理类*/
public class FightWinTargetHandler extends TaskTargetHandler {
    @Override
    public void handleEvent(Role role, BaseGameEvent baseGameEvent) {
        /**处理战斗胜利*/
        FightEndEvent fightEndEvent = (FightEndEvent)baseGameEvent;
        RoleTask roleTask = role.getTask();
        Integer taskId = roleTask.getTaskIdByFightId(fightEndEvent.getFightId());
        /**如果任务ID找不到，则说明该战斗并非由任务触发，不处理*/
        if (taskId == null) {return ;}

        NewTaskService taskService = SpringUtils.getBean(NewTaskService.class);
        TaskSet taskSet = taskService.getTaskSet(taskId, role);
        TaskTarget doingTaskTarget = taskService.getRoleDoingTaskTarget(role, taskSet);
        /**任务目标都完成时，可能获得null*/
        if (doingTaskTarget == null) {return ;}


        /**当前任务目标不是需要战斗胜利的目标时，不处理*/
        if (!taskService.isMatchTargetType(doingTaskTarget, TaskTargetType.FIGHT_WIN)) {return ;}

        taskService.updateTargetProgress(role, taskSet, doingTaskTarget);
    }

    /**立即触发任务相关的战斗*/
    @Override
    public void triggerNow(Role role, TaskSet taskSet, TaskTarget taskTarget) {
        try {
            if (SpringUtils.getTeamService().isInTeamAndNotLeader(role)) {
            //    LoggerFunction.TASK.getLogger().warn("{}({})不是队长触发战斗", role.getUid(), role.getName());
                return ;
            }
            FightWinTargetParam fightWinTargetParam = (FightWinTargetParam)taskTarget.getTaskTargetParam();
           // LoggerFunction.TASK.getLogger().info("{}({})开始触发战斗, 目标类型：{}", role.getUid(), role.getName(), taskTarget.getTaskTargetType());
            Fight fight = null;
            if (fightWinTargetParam.getFightTargetType() == FightWinTargetParam.PROTECT_FLYING_PET) {
                fight = SpringUtils.getBean(PetFlyTaskHandler.class).startFight(role, taskSet, fightWinTargetParam);

                if (fight == null) {
                    return ;
                }
            } else {
                fight = FightFactory.getFightFactory(Const.fightType_songshu).create(
                        0, role, fightWinTargetParam.getFightTargetType(), taskSet.getId(), fightWinTargetParam.getFightTarget());
            }
            fight.start();
//            LoggerFunction.TASK.getLogger().info("{}({})触发战斗完成, 目标类型：{}", role.getUid(), role.getName(), taskTarget.getTaskTargetType());

            /**缓存战斗ID与任务ID的对应关系*/
            role.getTask().addRecord4FightAndTask(fight.getId(), taskSet.getId());
        } catch (Exception e) {
            //LoggerFunction.TASK.getLogger().error("任务触发战斗异常"+role.getUid()+"("+role.getName()+")任务id:"+taskSet.getId(), e.getMessage(), e);
        }
    }
}
